package com.mcc.system.service.impl;

import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.common.core.domain.entity.SysDictData;
import com.mcc.common.utils.DictUtils;
import com.mcc.system.mapper.SysDictDataMapper;
import com.mcc.system.service.ISysDictDataService;

/**
 * 字典 业务层处理
 *
 * @author gxy
 */
@Service
public class SysDictDataServiceImpl implements ISysDictDataService {

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     *
     * @param dictData 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictData> selectDictDataList(SysDictData dictData) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(dictData)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(dictData.getDictType()), "dict_type", dictData.getDictType());
            queryWrapper.eq(ObjectUtil.isNotEmpty(dictData.getStatus()), "status", dictData.getStatus());
            queryWrapper.like(ObjectUtil.isNotEmpty(dictData.getDictLabel()), "dict_label", dictData.getDictLabel());
            queryWrapper.ge(ObjectUtil.isNotEmpty(dictData.getBeginTime()), "date_format(create_time,'%Y-%m-%d')", dictData.getBeginTime());
            queryWrapper.le(ObjectUtil.isNotEmpty(dictData.getEndTime()), "date_format(create_time,'%Y-%m-%d')", dictData.getEndTime());
        }
        queryWrapper.orderByAsc("dict_sort");
        return dictDataMapper.selectList(queryWrapper);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public SysDictData selectDictDataById(Long dictCode) {
        return dictDataMapper.selectById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     * @return 结果
     */
    @Override
    public int deleteDictDataByIds(Long[] dictCodes) {
        if (dictCodes.length == 0) return 0;
        int row = dictDataMapper.deleteBatchIds(ListUtil.toList(dictCodes));
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 新增保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(SysDictData dictData) {
        int row = dictDataMapper.insert(dictData);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param dictData 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(SysDictData dictData) {
        int row = dictDataMapper.updateById(dictData);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }
}
